package com.qf.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.qf.bean.ViewPagerEntity;
import com.qf.homeproject.R;

public class MyViewPagerAdapter extends PagerAdapter
{
	List<ViewPagerEntity> imgUrl;

	LayoutInflater inflater;
	private ImageLoader imageLoader;
	private DisplayImageOptions options = null;

	public MyViewPagerAdapter(List<ViewPagerEntity> imgList, Context context)
	{
		this.imgUrl = imgList;

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true).build();
		imageLoader = ImageLoader.getInstance();
	}

	@Override
	public int getCount()
	{
		return imgUrl.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1)
	{
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object)
	{
		container.removeView((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position)
	{
		View view = inflater.inflate(R.layout.item_viewpager_main, container,
				false);
		ImageView imageView_item = (ImageView) view
				.findViewById(R.id.imageView_item);

		imageLoader.displayImage(imgUrl.get(position).getPicurl(),
				imageView_item, options, new ImageLoadingListener()
				{

					@Override
					public void onLoadingStarted(String imageUri, View view)
					{

					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason)
					{

					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage)
					{

					}

					@Override
					public void onLoadingCancelled(String imageUri, View view)
					{
						// TODO Auto-generated method stub

					}
				});
		container.addView(view);
		return view;
	}
}
